package com.example.usercenter.controller;

import com.example.usercenter.common.BaseResponse;
import com.example.usercenter.common.ErrorCode;
import com.example.usercenter.common.ResultUtils;
import com.example.usercenter.exception.BusinessException;
import com.example.usercenter.model.domain.User;
import com.example.usercenter.model.domain.request.UserLoginRequest;
import com.example.usercenter.model.domain.request.UserRegisterRequest;
import com.example.usercenter.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;


import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;


/**
 * @projectName: user-center
 * @package: com.example.usercenter.controller
 * @className: UserController
 * @author: Dengke。Zhao
 * @description: TODO
 * @date: 2024/6/6 19:14
 * @version: 1.0
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param userRegisterRequest
     * @return
     */
    @PostMapping("/register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        //controller 层倾向于对请求参数本身的校验，不涉及业务逻辑本身（越少越好）
        //service 层是对业务逻辑的校验（有可能被 controller 之外的类调用）
        if (userRegisterRequest == null) {
//            return ResultUtils.error(ErrorCode.NULL_ERROR);
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userRegisterRequest.getUserAccount();
        String userPassword = userRegisterRequest.getUserPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        String planetCode = userRegisterRequest.getPlanetCode();
        if (StringUtils.isAnyBlank(userAccount, userPassword, checkPassword, planetCode)) {
//            return ResultUtils.error(ErrorCode.NULL_ERROR);
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        long result = userService.userRegister(userAccount, userPassword, checkPassword, planetCode);
        return ResultUtils.success(result);
    }

    /**
     * 用户登陆
     *
     * @param userLoginRequest
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/login")
    public BaseResponse<User> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest httpServletRequest) {
        //controller 层倾向于对请求参数本身的校验，不涉及业务逻辑本身（越少越好）
        //service 层是对业务逻辑的校验（有可能被 controller 之外的类调用）
        System.out.println("login");
        if (userLoginRequest == null) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        String userAccount = userLoginRequest.getUserAccount();
        String userPassword = userLoginRequest.getUserPassword();
        if (StringUtils.isAnyBlank(userAccount, userPassword)) {
            throw new BusinessException(ErrorCode.NULL_ERROR);
        }
        User user = userService.userLogin(userAccount, userPassword, httpServletRequest);
        return ResultUtils.success(user);
//        throw new BusinessException(ErrorCode.NULL_ERROR);
    }

    /**
     * 获取用户信息
     *
     * @param request
     * @return
     */
    @GetMapping("current")
    public BaseResponse<User> getCurrentUser(HttpServletRequest request) {
        User currentUser = userService.getCurrentUser(request);
//        return ResultUtils.error(ErrorCode.NULL_ERROR);
        return ResultUtils.success(currentUser);
    }

    /**
     * 搜索用户
     *
     * @param userName
     * @param httpServletRequest
     * @return
     */
    @GetMapping("/search")
    public BaseResponse<List<User>> userSearch(String userName, HttpServletRequest httpServletRequest) {
        List<User> users = userService.userSearchByUserName(userName, httpServletRequest);
        return ResultUtils.success(users);
    }

    /**
     * 删除用户
     *
     * @param id
     * @param httpServletRequest
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> userDeleteById(@RequestBody Long id, HttpServletRequest httpServletRequest) {
        if (id <= 0) {
            return ResultUtils.success(Boolean.FALSE);
        }
        Boolean result = userService.userDeleteById(id, httpServletRequest);
        return ResultUtils.success(result);
    }

    /**
     * 用户注销
     *
     * @param request
     * @return
     */
    @PostMapping("outLogin")
    public BaseResponse<Integer> userOutLogin(HttpServletRequest request) {
        if (request == null) {
//            return ResultUtils.success(null);
            return null;

        }
        Integer result = userService.userOutLogin(request);
        return ResultUtils.success(result);
    }
}

